package com.renshenlianzhuangyuan.h5;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import android.util.Log;
/**
 * ���Activity�࣬��Ŀ�����е�Activity��Ҫ�̳и���
 * 
 * @version v1.0 2014-03-22
 * @author Ц�Ǻ�
 * 
 */
public class ActivityCore extends FragmentActivity {
	private static Toast mToast;
	private OnBooleanListener onPermissionListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// �ѵ�ǰActivity�ŵ��ڴ����?
		AppMgr.getInstance().addApp(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// �ѵ�ǰActivity���ڴ����ɾ��?
		AppMgr.getInstance().finishApp(this);
	}

	public static void showToast(Context context, String msg) {
		if (mToast == null) {
			mToast = Toast.makeText(context, msg, 2000);
		} else {
			mToast.setText(msg);
		}
		mToast.show();
	}

	public void onPermissionRequests(String permission, OnBooleanListener onBooleanListener) {
		onPermissionListener = onBooleanListener;
		Log.d("MainActivity", "0");
		if (ContextCompat.checkSelfPermission(this,
				permission)
				!= PackageManager.PERMISSION_GRANTED) {
			// Should we show an explanation?
			Log.d("MainActivity", "1");
			if (ActivityCompat.shouldShowRequestPermissionRationale(this,
					Manifest.permission.READ_CONTACTS)) {
				//权限已有
				onPermissionListener.onClick(true);
			} else {
				//没有权限，申请一下
				ActivityCompat.requestPermissions(this,
						new String[]{permission},
						1);
			}
		}else{
			onPermissionListener.onClick(true);
			Log.d("MainActivity", "2"+ContextCompat.checkSelfPermission(this,
					permission));
		}
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		if (requestCode == 1) {
			if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
				//权限通过
				if (onPermissionListener != null) {
					onPermissionListener.onClick(true);
				}
			} else {
				//权限拒绝
				if (onPermissionListener != null) {
					onPermissionListener.onClick(false);
				}
			}
			return;
		}
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
	}
}
